<template>
  <div class="sidebar" :class="{ 'collapsed': isCollapsed }">
    <div class="sidebar-header">
      <div class="logo-container">
        <el-icon class="logo-icon"><Document /></el-icon>
        <span class="logo-text" v-show="!isCollapsed">文档管理系统</span>
      </div>
      <el-icon 
        class="collapse-icon"
        @click="toggleCollapse"
      >
        <component :is="isCollapsed ? 'Expand' : 'Fold'" />
      </el-icon>
    </div>

    <el-scrollbar>
      <el-menu
        :default-active="activeMenu"
        class="sidebar-menu"
        :collapse="isCollapsed"
        :collapse-transition="false"
        @select="handleSelect"
      >
        <el-menu-item index="/dashboard">
          <el-icon><HomeFilled /></el-icon>
          <template #title>
            <span>首页</span>
          </template>
        </el-menu-item>

        <el-sub-menu index="1">
          <template #title>
            <el-icon><Document /></el-icon>
            <span>文档管理</span>
          </template>
          <el-menu-item index="/dashboard/documents">全部文档</el-menu-item>
          <el-menu-item index="/dashboard/upload">上传文档</el-menu-item>
          <el-menu-item index="/dashboard/shared">共享文档</el-menu-item>
        </el-sub-menu>

        <el-menu-item index="/dashboard/notifications">
          <el-icon><Bell /></el-icon>
          <template #title>
            通知中心
            <el-badge 
              v-if="unreadCount > 0" 
              :value="unreadCount" 
              :max="99"
              class="notification-badge"
            />
          </template>
        </el-menu-item>

        <el-sub-menu index="2">
          <template #title>
            <el-icon><Setting /></el-icon>
            <span>系统设置</span>
          </template>
          <el-menu-item index="/dashboard/profile">个人信息</el-menu-item>
          <el-menu-item index="/dashboard/settings">系统设置</el-menu-item>
        </el-sub-menu>

        <el-menu-item index="/dashboard/about">
          <el-icon><InfoFilled /></el-icon>
          <template #title>关于系统</template>
        </el-menu-item>
      </el-menu>
    </el-scrollbar>

    <div class="sidebar-footer" @click="handleLogout">
      <el-icon><SwitchButton /></el-icon>
      <span v-show="!isCollapsed">退出登录</span>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import {
  Document,
  HomeFilled,
  Setting,
  User,
  Tools,
  InfoFilled,
  List,
  Upload,
  Share,
  Expand,
  Fold,
  SwitchButton,
  Bell
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

const isCollapsed = ref(false)
const activeMenu = computed(() => route.path)

// 模拟未读通知数量
const unreadCount = ref(3)

const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value
}

const handleSelect = (path) => {
  router.push(path)
}

const handleLogout = () => {
  ElMessageBox.confirm(
    '确定要退出登录吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    localStorage.removeItem('access_token')
    localStorage.removeItem('refresh_token')
    localStorage.removeItem('username')
    router.push('/login')
    ElMessage.success('已成功退出登录')
  }).catch(() => {})
}
</script>

<style scoped>
.sidebar {
  height: 100vh;
  width: 240px;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  border-right: 1px solid rgba(255, 255, 255, 0.1);
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

.sidebar.collapsed {
  width: 64px;
}

.sidebar-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.logo-container {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  font-size: 24px;
  color: var(--el-color-primary);
}

.logo-text {
  font-size: 18px;
  font-weight: 600;
  color: white;
  white-space: nowrap;
}

.collapse-icon {
  cursor: pointer;
  font-size: 20px;
  color: rgba(255, 255, 255, 0.7);
  transition: all 0.3s ease;
}

.collapse-icon:hover {
  color: white;
  transform: scale(1.1);
}

.sidebar-menu {
  flex: 1;
  border: none;
  background: transparent;
}

:deep(.el-menu) {
  border: none !important;
  background: transparent;
}

:deep(.el-menu-item),
:deep(.el-sub-menu__title) {
  height: 50px;
  line-height: 50px;
  color: rgba(255, 255, 255, 0.7) !important;
  background: transparent !important;
}

:deep(.el-menu-item:hover),
:deep(.el-sub-menu__title:hover) {
  color: white !important;
  background: rgba(255, 255, 255, 0.1) !important;
}

:deep(.el-menu-item.is-active) {
  color: var(--el-color-primary) !important;
  background: rgba(107, 70, 193, 0.2) !important;
}

:deep(.el-menu-item .el-icon),
:deep(.el-sub-menu__title .el-icon) {
  font-size: 18px;
}

.sidebar-footer {
  height: 50px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  cursor: pointer;
  color: rgba(255, 255, 255, 0.7);
  transition: all 0.3s ease;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-footer:hover {
  color: white;
  background: rgba(255, 255, 255, 0.1);
}

/* 添加动画效果 */
:deep(.el-menu-item),
:deep(.el-sub-menu__title) {
  position: relative;
  overflow: hidden;
}

:deep(.el-menu-item)::before,
:deep(.el-sub-menu__title)::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    120deg,
    transparent,
    rgba(255, 255, 255, 0.1),
    transparent
  );
  transform: translateX(-100%);
}

:deep(.el-menu-item:hover)::before,
:deep(.el-sub-menu__title:hover)::before {
  animation: shine 0.8s;
}

@keyframes shine {
  100% {
    transform: translateX(100%);
  }
}

.notification-badge {
  margin-left: 8px;
}

:deep(.el-badge__content) {
  transform: scale(0.8);
}
</style> 